package com.jvk.check4home;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.dreasyLib.comm.MessageUtils;
import com.jvk.check4home.frwk.OutputState;
import com.jvk.check4home.frwk.constants;
import com.jvk.check4home.frwk.frwk;

public class configurationActivity extends Activity {
    /** Called when the activity is first created. */
	private String user="";
	private String pass="";
	private String filename="";
	private String spreedname="";
	
	protected LinearLayout myLayout;
	protected EditText EditUser;
	protected EditText EditPassword;
	protected EditText EditFilename;
	protected EditText EditSpreedname;
	protected Button   SaveLoad;
	protected Button   Quit;
	protected OnClickListener OnClickLoad;
	protected OnClickListener OnClickQuit;

	
	private void initListeners() {
		OnClickLoad=new OnClickListener() {
			@Override
			public void onClick(View v) {
				frwk.getInstance(getApplicationContext()).reload=true;
				lauchList();
			}
		};
		OnClickQuit=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				configurationActivity.this.finish();
				
			}
		};
		Quit.setOnClickListener(OnClickQuit);
		SaveLoad.setOnClickListener(OnClickLoad);
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (isTaskRoot())
		{
			configurationActivity.this.finish();
		}
	}

	private void initXMLElements() {
		EditUser=(EditText) findViewById(R.id.config_edit_user);
		EditPassword=(EditText) findViewById(R.id.config_edit_pass);
		EditFilename=(EditText) findViewById(R.id.config_edit_filename);
		EditSpreedname=(EditText) findViewById(R.id.config_edit_spreedname);
		SaveLoad=(Button) findViewById(R.id.config_btn_save);
		Quit=(Button) findViewById(R.id.config_btn_quit);
		myLayout=(LinearLayout)findViewById(R.id.main_layout);
	}
	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        initXMLElements();
        initListeners();
        user=frwk.getInstance(getApplicationContext()).shareManager.LoadConfig(constants.USER);
        pass=frwk.getInstance(getApplicationContext()).shareManager.LoadConfig(constants.PASWORD);
        filename=frwk.getInstance(getApplicationContext()).shareManager.LoadConfig(constants.PAGE_NAME);
        spreedname=frwk.getInstance(getApplicationContext()).shareManager.LoadConfig(constants.SPREED_NAME);
        if (filename.equals(""))filename=constants.PAGE_NAME;
        if (spreedname.equals(""))spreedname=constants.SPREED_NAME;
        EditFilename.setText(filename);
        EditSpreedname.setText(spreedname);
        EditUser.setText(user);
        EditPassword.setText(pass);
        if (isTaskRoot())
        {
        	myLayout.setVisibility(View.INVISIBLE);
        	fillAndBoot();
        }else
        {
        }
        
    }

	public void setVisible() {
		myLayout.setVisibility(View.VISIBLE);
		
	}

	private void fillAndBoot() {
		if (user.equals("")||pass.equals(""))
        {//TODO sacar el formulario para rellenar
        	myLayout.setVisibility(View.VISIBLE);
        }else
        {
	        lauchList();
        }
		
	}

	private void lauchList() {//Check if all are filled and load it showing a dialog
		user=EditUser.getText().toString();
		pass=EditPassword.getText().toString();
		filename=EditFilename.getText().toString();
		spreedname=EditSpreedname.getText().toString();
		
		if ((user.length()>0)&&
			(pass.length()>0)&&
			(filename.length()>0)&&
			(spreedname.length()>0)
		   )
		{
			frwk.getInstance(getApplicationContext()).reload=true;
			frwk.getInstance(getApplicationContext()).setConfiguration(user, pass, filename, spreedname);
			Intent intent = new Intent(configurationActivity.this, HomeList.class);			
			startActivity(intent);
		}else
		{//show dialog, something wrong
			
		}
		
	}


   


}